<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      div {
        width: 300px;
        height: 300px;
        margin: 100px auto;
        /* background: url(1.jpg); */
      }
    </style>
  </head>
  <body>
    <!-- 准备几个图片，实现点击按钮随机将一个盒子的背景随机切换到几个图片中的一个 -->
    <div class="box"></div>
    <button>点击有惊喜</button>

    <script>
      var btns = document.querySelector("button");
      var boxEle = document.querySelector(".box");

      btns.onclick = function () {
        var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
        var res = parseInt(Math.random() * 10);
        arr.forEach(function (item) {
          console.log(res);
          if (item == res) {
            boxEle.style.background = `url(images/${item}.jpg) no-repeat center/100%`;
            // document.boxEle.style.backgroundSize = `100vw 100vh`;
          }
        });
      };
    </script>
  </body>
</html>
